var report = {
	
	sla_process_init : function(){
		$(document.search_form.group_code).on('change', function(event){
			report.get_group_user();
		});
		
		$(document.search_form.from_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.to_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.month).on('change', function(event){
			report.clear_period_filter();
		});
		
		$(document.search_form.year).on('keyup', function(event){
			report.clear_period_filter();
		});
	},
	
	clear_month_filter : function(){
		$(document.search_form.month).val('');
		$(document.search_form.year).val('');
	},
	
	clear_period_filter : function(){
		$(document.search_form.to_date).val('');
		$(document.search_form.from_date).val('');
	},

	report_init : function(){
		if (document.search_form.year.value != "" || document.search_form.month.value != ""){
			document.search_form.from_date.disabled = true;
			document.search_form.to_date.disabled = true;
		} else {
			document.search_form.from_date.disabled = false;
			document.search_form.to_date.disabled = false;
		}
		
		if (document.search_form.from_date.value != "" || document.search_form.to_date.value != ""){
			document.search_form.year.disabled = true;
			document.search_form.month.disabled = true;
		} else {
			document.search_form.year.disabled = false;
			document.search_form.month.disabled = false;
		}
		
		var elmt;
		//If monthly field not empty, disable daily field
		$(document.search_form.year).on('change', function(event){
			if ($(this).val() != "" ){
				elmt = document.search_form.from_date;
				elmt.disabled = true;
				elmt = document.search_form.to_date;
				elmt.disabled = true;
			} else {
				elmt = document.search_form.from_date;
				elmt.disabled = false;
				elmt = document.search_form.to_date;
				elmt.disabled = false;
			}
		});
		$(document.search_form.month).on('change', function(event){
			if ($(this).val() != "" ){
				elmt = document.search_form.from_date;
				elmt.disabled = true;
				elmt = document.search_form.to_date;
				elmt.disabled = true;
			} else {
				elmt = document.search_form.from_date;
				elmt.disabled = false;
				elmt = document.search_form.to_date;
				elmt.disabled = false;
			}
		});
		
		//If daily field not empty, disable monthly field
		$(document.search_form.from_date).on('change', function(event){
			if ($(this).val() != "" ){
				elmt = document.search_form.year;
				elmt.disabled = true;
				elmt = document.search_form.month;
				elmt.disabled = true;
			} else {
				elmt = document.search_form.year;
				elmt.disabled = false;
				elmt = document.search_form.month;
				elmt.disabled = false;
			}
		});
		$(document.search_form.to_date).on('change', function(event){
			if ($(this).val() != "" ){
				elmt = document.search_form.year;
				elmt.disabled = true;
				elmt = document.search_form.month;
				elmt.disabled = true;
			} else {
				elmt = document.search_form.year;
				elmt.disabled = false;
				elmt = document.search_form.month;
				elmt.disabled = false;
			}
		});
	},
	
	get_group_user : function(){
		var elmt, postdata;
		
		elmt = document.search_form.group_code;
		postdata = 'group_code=' + encodeURIComponent(elmt.value);
		
		var callback = function(json){
            if(json.status == 'OK'){
            	
            	var html = '<option value=""></option>';
            	
            	for(var n in json.group_users){
            		var item = json.group_users[n];
            		html += '<option value="' + item.id + '">' + item.name + '</option>';
            	}
            	
            	$(document.search_form.group_user).html(html);
            	
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'GET',
            'url' : jq_main_url + '/Report.do?dispatch=get_group_user',
            'postdata' : postdata
        };

        mapUI.jq_call(jq, callback);
	},
	
	search_sla_process_validate : function(){
		var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
		
		var from_date = document.search_form.from_date.value;
		var to_date = document.search_form.to_date.value;
		var year = document.search_form.year.value;
		var month = document.search_form.month.value;
		
		if(from_date.length > 0 && regex.test(from_date) == false) {	
			mapUI.jq_message('Invalid FROM DATE format.', 10);
			return false;
		}
			
		if(from_date.length > 0) {	
			var parts = from_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('FROM DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(to_date.length > 0 && regex.test(to_date) == false) {	
			mapUI.jq_message('Invalid TO DATE format.', 10);
			return false;
		}
		
		if(to_date.length > 0) {	
			var parts = to_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('TO DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(year.length > 0) {
			if(year.length != 4 || isNaN(year)) {
				mapUI.jq_message('Invalid YEAR.', 10);
				return false;
			} else if(year > new Date().getFullYear()) {
				mapUI.jq_message('YEAR should not be greater than current Year.', 10);
				return false;
			}
		}
		
		
		if(from_date.length > 0 && to_date.length <= 0) {
			mapUI.jq_message('Please fill in TO DATE.', 10);
			return false;
		}
		

		if(to_date.length > 0 && from_date.length <= 0) {
			mapUI.jq_message('Please fill in FROM DATE.', 10);
			return false;
		}
		
		if(month.length > 0 && year.length <= 0) {
			mapUI.jq_message('Please fill in YEAR.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length <= 0) {
			mapUI.jq_message('Please choose a MONTH.', 10);
			return false;
		}
		
		if(from_date.length <= 0 && to_date.length <= 0 && month.length <= 0 && year.length <= 0) {
			mapUI.jq_message('Please specify a PERIOD or a MONTH.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length > 0 && year == new Date().getFullYear() && month > new Date().getMonth()+1) {
			mapUI.jq_message('MONTH should not be greater than current Month.', 10);
			return false;
		}
		
		return true;
	},
	
	external_sla_process_init : function(){
		$(document.search_form.group_code).on('change', function(event){
			report.get_group_user();
		});
		
		$(document.search_form.from_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.to_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.month).on('change', function(event){
			report.clear_period_filter();
		});
		
		$(document.search_form.year).on('keyup', function(event){
			report.clear_period_filter();
		});
	},
	
	search_external_sla_process_validate : function(){
		var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
		
		var from_date = document.search_form.from_date.value;
		var to_date = document.search_form.to_date.value;
		var year = document.search_form.year.value;
		var month = document.search_form.month.value;
		
		if(from_date.length > 0 && regex.test(from_date) == false) {	
			mapUI.jq_message('Invalid FROM DATE format.', 10);
			return false;
		}
			
		if(from_date.length > 0) {	
			var parts = from_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('FROM DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(to_date.length > 0 && regex.test(to_date) == false) {	
			mapUI.jq_message('Invalid TO DATE format.', 10);
			return false;
		}
		
		if(to_date.length > 0) {	
			var parts = to_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('TO DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(year.length > 0) {
			if(year.length != 4 || isNaN(year)) {
				mapUI.jq_message('Invalid YEAR.', 10);
				return false;
			} else if(year > new Date().getFullYear()) {
				mapUI.jq_message('YEAR should not be greater than current Year.', 10);
				return false;
			}
		}
		
		
		if(from_date.length > 0 && to_date.length <= 0) {
			mapUI.jq_message('Please fill in TO DATE.', 10);
			return false;
		}
		

		if(to_date.length > 0 && from_date.length <= 0) {
			mapUI.jq_message('Please fill in FROM DATE.', 10);
			return false;
		}
		
		if(month.length > 0 && year.length <= 0) {
			mapUI.jq_message('Please fill in YEAR.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length <= 0) {
			mapUI.jq_message('Please choose a MONTH.', 10);
			return false;
		}
		
		if(from_date.length <= 0 && to_date.length <= 0 && month.length <= 0 && year.length <= 0) {
			mapUI.jq_message('Please specify a PERIOD or a MONTH.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length > 0 && year == new Date().getFullYear() && month > new Date().getMonth()+1) {
			mapUI.jq_message('MONTH should not be greater than current Month.', 10);
			return false;
		}
		
		return true;
	},
	
	region_sla_init : function(){
		$(document.search_form.group_code).on('change', function(event){
			report.get_group_user();
		});
		
		$(document.search_form.from_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.to_date).on('keyup', function(event){
			report.clear_month_filter();
		});
		
		$(document.search_form.month).on('change', function(event){
			report.clear_period_filter();
		});
		
		$(document.search_form.year).on('keyup', function(event){
			report.clear_period_filter();
		});
	},
	
	search_region_sla_validate : function(){
		var regex = /^(\d{2})\/(\d{2})\/(\d{4})$/;
		
		var from_date = document.search_form.from_date.value;
		var to_date = document.search_form.to_date.value;
		var year = document.search_form.year.value;
		var month = document.search_form.month.value;
		
		if(from_date.length > 0 && regex.test(from_date) == false) {	
			mapUI.jq_message('Invalid FROM DATE format.', 10);
			return false;
		}
			
		if(from_date.length > 0) {	
			var parts = from_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('FROM DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(to_date.length > 0 && regex.test(to_date) == false) {	
			mapUI.jq_message('Invalid TO DATE format.', 10);
			return false;
		}
		
		if(to_date.length > 0) {	
			var parts = to_date.split('/');
			var date = new Date(parts[2], parts[1]-1, parts[0]); //please put attention to the month
			
			if(date > new Date()) {
				mapUI.jq_message('TO DATE should not be greater than Today.', 10);
				return false;
			}
		}
		
		if(year.length > 0) {
			if(year.length != 4 || isNaN(year)) {
				mapUI.jq_message('Invalid YEAR.', 10);
				return false;
			} else if(year > new Date().getFullYear()) {
				mapUI.jq_message('YEAR should not be greater than current Year.', 10);
				return false;
			}
		}
		
		
		if(from_date.length > 0 && to_date.length <= 0) {
			mapUI.jq_message('Please fill in TO DATE.', 10);
			return false;
		}
		

		if(to_date.length > 0 && from_date.length <= 0) {
			mapUI.jq_message('Please fill in FROM DATE.', 10);
			return false;
		}
		
		if(month.length > 0 && year.length <= 0) {
			mapUI.jq_message('Please fill in YEAR.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length <= 0) {
			mapUI.jq_message('Please choose a MONTH.', 10);
			return false;
		}
		
		if(from_date.length <= 0 && to_date.length <= 0 && month.length <= 0 && year.length <= 0) {
			mapUI.jq_message('Please specify a PERIOD or a MONTH.', 10);
			return false;
		}
		
		if(year.length > 0 && month.length > 0 && year == new Date().getFullYear() && month > new Date().getMonth()+1) {
			mapUI.jq_message('MONTH should not be greater than current Month.', 10);
			return false;
		}
		
		return true;
	},
	
	change_matrix : function(){
		var selected_application = $(document.search_form.application_type).val();
		
		if("New Application" == selected_application){
			document.search_form.matrix.disabled=false;
		}else{
			document.search_form.matrix.disabled=true;
		}
	},
};